package com.gy.common.util;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.gy.common.exception.OptionalException;
import com.gy.common.model.vo.JsonToData;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

public class ZTUtil<T> {
    public Integer getTotal(String json) {


        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);
        objectMapper.configure(JsonGenerator.Feature.IGNORE_UNKNOWN, true);
        try {
            //JsonFileUtils.readJsonFile("C:\\Users\\Administrator\\Desktop\\test.json")
            JsonToData regionLoadExtensionProjectJsonToData = objectMapper.readValue(json,
                    new TypeReference<JsonToData>() {
                    });

//            System.out.println("_______________" + regionLoadExtensionProjectJsonToData.getData());

            JsonToData.ObjectData data = regionLoadExtensionProjectJsonToData.getData();
            return data.getTotalNum();
        } catch (Exception e) {
            e.printStackTrace();
            throw new OptionalException(10000, "类型转换错误");
        }


    }



    public List<T> getData(String json, Class clazz) {
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);
        objectMapper.configure(JsonGenerator.Feature.IGNORE_UNKNOWN, true);
        try {
            //JsonFileUtils.readJsonFile("C:\\Users\\Administrator\\Desktop\\test.json")
            JsonToData regionLoadExtensionProjectJsonToData = objectMapper.readValue(json,
                    new TypeReference<JsonToData>() {
                    });

            System.out.println("_______________获取数据" );//+ regionLoadExtensionProjectJsonToData.getData()

            JsonToData.ObjectData data = regionLoadExtensionProjectJsonToData.getData();
            List<Map<String, Object>> rows = data.getRows();

            List<T> list = new ArrayList<>();
            for (Map<String, Object> row : rows) {
                List<String> clone = new ArrayList<>();
                Set<String> keySet = row.keySet();
                for (String s : keySet) {
                    clone.add(s + "");
                }

                for (String key : clone) {
                    String s = key.toLowerCase(Locale.ROOT);
                    row.put(HumpLineUtil.lineToHump(s), row.get(key));
//                    row.remove(s);
                }
                list.add(map2Object(row, clazz));
            }

            return list;
        } catch (Exception e) {
            e.printStackTrace();
            throw new OptionalException(10000, "类型转换错误");
        }
    }

    public T map2Object(Map map, Class clazz) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat sdf3 = new SimpleDateFormat("yyyyMMdd");

        if (map == null) {
            return null;

        }

        T obj = null;

        try {
// 使用newInstance来创建对象

            obj = (T) clazz.newInstance();

// 获取类中的所有字段

            Field[] fields = obj.getClass().getDeclaredFields();

            for (Field field : fields) {
                int mod = field.getModifiers();

// 判断是拥有某个修饰符

                if (Modifier.isStatic(mod) || Modifier.isFinal(mod)) {
                    continue;

                }

// 当字段使用private修饰时，需要加上

                field.setAccessible(true);

// 获取参数类型名字

                String filedTypeName = field.getType().getName();

// 判断是否为时间类型，使用equalsIgnoreCase比较字符串，不区分大小写

// 给obj的属性赋值
                if (map.get(field.getName()) != null) {
                    if (filedTypeName.equalsIgnoreCase("java.util.date")) {
                        String datetimestamp = (String) map.get(field.getName());

                        if (datetimestamp.equalsIgnoreCase("null")) {
                            field.set(obj, null);

                        } else {
                            Date parse = null;
                            try{
                                 parse = sdf.parse(datetimestamp);
                            }catch (Exception e){
                                try{
                                    parse = sdf2.parse(datetimestamp);
                                }catch (Exception e1){
                                        parse = sdf3.parse(datetimestamp);

                                }
                            }



                            field.set(obj, parse);

                        }
                    } else if (field.getType().isAssignableFrom(BigDecimal.class)) {
                        field.set(obj, new BigDecimal(map.get(field.getName()).toString()));
                    } else {
                        field.set(obj, map.get(field.getName()));
                    }
                }
            }

        } catch (Exception e) {
            e.printStackTrace();

        }

        return obj;

    }
}
